package org.ithink.util;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.Socket;
import java.net.URL;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

public class ConnectTest {

  /**
   * @param args
   */
  public static void main(String[] args) {
    System.out.println(testConnection("192.168.0.19", 8080));
    System.out.println(getUrlConnection("http://www.baidu.com1"));
    System.out.println(getUrlsConnection("https://www.cebbank.com1/"));
  }
  
  /**
   * 
   * @param ip
   * @param port
   * @return
   */
  public static boolean testConnection(String ip, int port){
    try {
      Socket clientSocket = new Socket(ip, port);
      clientSocket.close();
      return true;
    } catch (Exception e) {
      return false;
    }
  }

  /**
   * 
   * @param urlString
   * @return
   */
  public static int getUrlConnection(String urlString) {
    int connectionInt = 500;
    try {
      URL url = new URL(urlString);
      HttpURLConnection conn = (HttpURLConnection) url.openConnection();
      int size = conn.getHeaderFields().size();
      if (size > 0) {
        connectionInt = conn.getResponseCode();
      }
    } catch (IOException x) {
      x.printStackTrace();
    }
    return connectionInt;
  }
  
  /**
   * 
   * @param urlString
   * @return
   */
  public static int getUrlsConnection(String urlString) {
    int connectionInt =500;
      try {
       SSLContext sc = SSLContext.getInstance("SSL");
       sc.init(null, new TrustManager[] { new TrustAnyTrustManager() },
         new java.security.SecureRandom());
       URL console = new URL(
         urlString);
       HttpsURLConnection conn = (HttpsURLConnection) console
         .openConnection();
       conn.setSSLSocketFactory(sc.getSocketFactory());
       conn.setHostnameVerifier(new TrustAnyHostnameVerifier());

       int size = conn.getHeaderFields().size();      
       if(size>0){
        connectionInt = conn.getResponseCode();
       }
     
       conn.disconnect();
      } catch (Exception e) {
       System.out.println("HTTPS网站无效或无法访问!");
       System.out.println(e);
      }
      return connectionInt ;
   }
  
  private static class TrustAnyTrustManager implements X509TrustManager {
    public void checkClientTrusted(X509Certificate[] chain, String authType)
      throws CertificateException {
    }
    public void checkServerTrusted(X509Certificate[] chain, String authType)
      throws CertificateException {
    }
    public X509Certificate[] getAcceptedIssuers() {
     return new X509Certificate[] {};
    }
  }
  
  private static class TrustAnyHostnameVerifier implements HostnameVerifier {
    public boolean verify(String hostname, SSLSession session) {
     return true;
    }
  }

}